import org.junit.Test;

import utl.mofen.jsonStreamHandler.HandlerFactory;
import utl.mofen.jsonStreamHandler.IJSonStreamHandler;
import utl.mofen.objects.JSonStream;
import utl.mofen.objects.KeyStream;
import utl.mofen.objects.KeyStreamCoder;
import utl.mofen.utils.GsonUtil;
import utl.mofen.utils.ResLoader;

import com.google.gson.JsonObject;


public class JSonStreamDBHandlerTester {
	@Test
	public void configLoaderTester() throws Exception
	{
		String s = ResLoader.getString("/script/configLoader.txt");
		System.out.println(s);
		JSonStream jsonStream = new JSonStream();
		JsonObject jsonObject = GsonUtil.parseObject(s);
		jsonStream.setJsonObject(jsonObject);		
		HandlerFactory.checkInitialize(null);
		
		IJSonStreamHandler handler = HandlerFactory.createHandler(jsonStream);
		handler.setJSonStream(jsonStream);
		handler.doAction();
		
		s = new String(jsonStream.getStream(), "utf-8");
		System.out.println(s);
	}
	
	@Test
	public void JSonStreamHandler_OpenSQL() throws Exception
	{
		String s = ResLoader.getString("/script/JSonStreamDBHandler_OpenSQL.txt");
		System.out.println(s);
		JSonStream jsonStream = new JSonStream();
		jsonStream.initialize();
		JsonObject jsonObject = GsonUtil.parseObject(s);
		jsonStream.setJsonObject(jsonObject);		
		HandlerFactory.checkInitialize(null);
		
		IJSonStreamHandler handler = HandlerFactory.createHandler(jsonStream);
		handler.setJSonStream(jsonStream);
		handler.doAction();
		
		
		KeyStream keyStream = KeyStreamCoder.decodeFromByte(jsonStream.getStream());	
		
		s = new String(keyStream.getStream("detail"), "utf-8");
		System.out.println(s);
	}
	
	@Test
	public void sTester()
	{
		try {
			try {
				throw new Exception("出错!");
			} finally {
				System.out.println("xxx");
			}
		} catch (Exception e) {
			System.out.println("catched");
		}
		
	}
	
	@Test
	public void JSonStreamHandler_CDSUpdate() throws Exception
	{
		String s = ResLoader.getString("/script/JSonStreamDBHandler_CDSUpdate.txt");
		System.out.println(s);
		JSonStream jsonStream = new JSonStream();
		jsonStream.initialize();
		JsonObject jsonObject = GsonUtil.parseObject(s);
		jsonStream.setJsonObject(jsonObject);
		
		//data 该数据可以使用 SQLDataSync.exe制作 Delphi中的打包数据
		String data = ResLoader.getString("/script/Delta.txt");		
		KeyStream keyStream = new KeyStream();
		keyStream.add("main", data.getBytes("utf-8")); 		
		jsonStream.setStream(KeyStreamCoder.encode2Byte(keyStream));
		
		
		HandlerFactory.checkInitialize(null);
		
		IJSonStreamHandler handler = HandlerFactory.createHandler(jsonStream);
		handler.setJSonStream(jsonStream);
		handler.doAction();
	}
}
